<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SwtRequestLog;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;

class SwtRequestLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SwtRequestLog(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('log_type')->using([1 => '大黄鹅', 2 => '宝坚', 3 => '拓源']);
            $grid->column('request_id')->copyable();
            $grid->column('path');
            $grid->column('input')->display('详情')->expand(function () {
                $content = htmlspecialchars(json_encode(json_decode($this->input), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
                $card = new Card(null, "<pre style='text-align: start'>".$content."</pre>");
                return  "<div style='padding:10px 10px 0'>$card</div>";
            });

            $grid->column('created_at') -> format('Y-m-d H:i:s');

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('log_type', '厂家', [
                    1 => '大黄鹅',
                    2 => '宝坚',
                    3 => '拓源'
                ]);
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('request_id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SwtRequestLog(), function (Show $show) {
            $show->field('id');
            $show->field('log_type');
            $show->field('request_id');
            $show->field('path');
            $show->field('input');
            $show->field('created_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SwtRequestLog(), function (Form $form) {
            $form->display('id');
            $form->text('log_type');
            $form->text('request_id');
            $form->text('path');
            $form->text('input');
            $form->text('created_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
